package com.ldc.framework.fragment

import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.ldc.commonlibs.conf.AppConf
import com.ldc.commonlibs.conf.RouteConf
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.framework.adapter.NaviFragmentAdapter
import com.ldc.framework.databinding.FrameworkFragmentNavBinding
import com.ldc.framework.model.NavArticleModel
import com.ldc.framework.viewmodel.FrameworkViewModelBase


class NavFragment : VBMFragment<FrameworkFragmentNavBinding, FrameworkViewModelBase>() {
    companion object {
        private const val REFRESH_CODE = 0x123
        fun newInstance(): NavFragment = NavFragment().also {
            it.arguments = Bundle()
        }
    }


    private var naviFragmentAdapter: NaviFragmentAdapter? = null

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getNaviJson()
            }
        }
        return super.processMessage(msg)
    }

    override fun main() {
        initAdapter()
        viewBind?.contentView?.also {
            it.refreshView.setEnableLoadMore(false)
            it.refreshView.setOnRefreshListener {
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        }
        viewBind?.statusView?.let {
            it.showLoadView()
            it.addRetryClickListener(object : OnRowListener<Int> {
                override fun onRow(v: View, data: Int) {
                    it.showLoadView("正在重试，请稍等")
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)


    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FrameworkFragmentNavBinding =
        FrameworkFragmentNavBinding.inflate(inflater, container, false)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)

    private fun initAdapter() {
        naviFragmentAdapter = NaviFragmentAdapter().also {
            it.addOnRowListener(object :
                OnRowListener<NavArticleModel.NavArticleModelItem.Article> {
                override fun onRow(v: View, data: NavArticleModel.NavArticleModelItem.Article) {
                    ARouter.getInstance().build(RouteConf.WebViewRoute.startEnter)
                        .withString(AppConf.ARG_TITLE, data.title)
                        .withString(AppConf.ARG_URL, data.link)
                        .navigation()
                }
            })
        }
        viewBind?.contentView?.rv?.recyclerview?.also { rv ->
            rv.adapter = naviFragmentAdapter
            rv.setHasFixedSize(true)
            rv.layoutManager = LinearLayoutManager(rv.context, LinearLayoutManager.VERTICAL, false)
        }
    }

    private fun getNaviJson() {
        viewModel?.NaviJson()?.observe(this) {
            viewBind?.statusView?.showContentView()
            viewBind?.contentView?.refreshView?.also { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                //
                if (it.data.isNullOrEmpty()) {
                    viewBind?.statusView?.showEmptyView()
                } else {
                    naviFragmentAdapter?.newData(it.data)
                }
            } else {
                viewBind?.statusView?.showErrorView()
            }
        }
    }

}